﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.DataAccess
{
    public class DaysOfWeekProvider : ObservableCollection<UIElement>
    {
        #region Variables
        private ucDaysOfWeek Owner = null;
        #endregion

        #region Constructor
        public DaysOfWeekProvider(ucDaysOfWeek Owner)
        {
            this.Owner = Owner;
        }
        #endregion

        #region Methods
        #region GetDaysOfWeek
        public ObservableCollection<UIElement> GetDaysOfWeek()
        {
            base.Clear();

            ucDayOfWeekContentItem _DayOfWeek = null;

            switch (Owner.CurrentDateView)
            {
                case Enumerations.DateView.Day:
                    _DayOfWeek = new ucDayOfWeekContentItem();
                    _DayOfWeek.Day = Owner.CurrentDate.Day.ToString();
                    //_DayOfWeek.DayString = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)Owner.CurrentDate.DayOfWeek].ToString();
                    _DayOfWeek.DayString = Owner.CurrentDate.DayOfWeek.ToString();
                    _DayOfWeek.Month = Owner.CurrentDate.Month.ToString();
                    _DayOfWeek.Year = Owner.CurrentDate.Year.ToString();
                    _DayOfWeek.DisplayDay = Visibility.Visible;
                    _DayOfWeek.Cursor = Cursors.Arrow;

                    SetHeaderBackground(ref _DayOfWeek, _DayOfWeek.Date);
                    this.Add(_DayOfWeek);
                    break;
                case Enumerations.DateView.WorkWeek:
                case Enumerations.DateView.FullWeek:
                    int DayOffSet = 0;

                    if (Owner.CurrentDateView == Enumerations.DateView.WorkWeek)
                        DayOffSet = 5;
                    else if (Owner.CurrentDateView == Enumerations.DateView.FullWeek)
                        DayOffSet = 7;

                    DateTime _StartDate = Owner.CurrentDate.AddDays((int)System.DayOfWeek.Monday - (int)Owner.CurrentDate.DayOfWeek);

                    for (int i = 0; i < DayOffSet; i++)
                    {
                        _DayOfWeek = new ucDayOfWeekContentItem();
                        _DayOfWeek.Day = _StartDate.AddDays(i).Day.ToString();
                        _DayOfWeek.DayString = _StartDate.AddDays(i).DayOfWeek.ToString();
                        _DayOfWeek.Month = _StartDate.Month.ToString();
                        _DayOfWeek.Year = _StartDate.Year.ToString();
                        _DayOfWeek.DisplayDay = Visibility.Visible;
                        _DayOfWeek.Cursor = Cursors.Hand;

                        SetHeaderBackground(ref _DayOfWeek, _DayOfWeek.Date);
                        this.Add(_DayOfWeek);
                    }
                    break;
                case Enumerations.DateView.Month:
                case Enumerations.DateView.Year:
                    for (int i = 0; i < 7; i++)
                    {
                        _DayOfWeek = new ucDayOfWeekContentItem();
                        _DayOfWeek.Day = (i + 1).ToString();
                        _DayOfWeek.DayString = Enum.Parse(typeof(System.DayOfWeek), i.ToString(), true).ToString();
                        _DayOfWeek.Month = Owner.CurrentDate.Month.ToString();
                        _DayOfWeek.Year = Owner.CurrentDate.Year.ToString();
                        _DayOfWeek.DisplayDay = Visibility.Collapsed;
                        _DayOfWeek.Cursor = Cursors.Arrow;

                        SetHeaderBackground(ref _DayOfWeek, _DayOfWeek.Date);
                        this.Add(_DayOfWeek);
                    }
                    break;
            }

            return this;
        }
        #endregion

        #region SetHeaderBackground
        private void SetHeaderBackground(ref ucDayOfWeekContentItem brd, DateTime _Date)
        {
            if (_Date.ToShortDateString() == DateTime.Today.ToShortDateString())
            {
                brd.Background = Owner.CurrentDateBackground;
                brd.BorderBrush = Owner.CurrentDateBorderBrush;
                brd.BorderThickness = Owner.CurrentDateBorderThickness;
            }
            else
            {
                brd.Background = Owner.ContentBackground;
                brd.BorderBrush = Owner.ContentBorderBrush;
                brd.BorderThickness = Owner.ContentBorderThickness;
            }
        }
        #endregion
        #endregion
    }
}
